home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 04 Pathfinding and Movement / 05 Hancock / Goal_GoThroughDoor.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-08-19  |  1.6 KB  |  56 lines

  1.  
  2. #include "GoThroughDoor.h"
  3. #include "Goal_GotoNode.h"
  4. #include "Goal_FollowLink.h"
  5. #include "NodeMapPrims.h"
  6. #include "Goal_HitDoorSwitch.h"
  7. #include "Door.h"
  8.  
  9. Goal_GoThroughDoor::Goal_GoThroughDoor(AI* pAI, const PathLink *pathlink)
  10.                                         : Goal( pAI ), GoalQueue(), link(pathlink), active(false)
  11. {
  12.     LAssert(link->flags & PathLink::flagLinkDoor); //make sure the link was labeled with a door
  13.     door = link->GetDoor();
  14.     LAssert(door);
  15. }
  16.  
  17. Goal_GoThroughDoor::~Goal_GoThroughDoor()
  18. {
  19. }
  20.  
  21. bool Goal_GoThroughDoor::ReplanSubgoals(){
  22.  
  23.     Door::DoorStatus doorStatus = door->GetDoorStatus();
  24.  
  25.     const PathNode *bestswitchnode = NULL;
  26.     const Switch *bestswitch = NULL;
  27.     //Now find the switch that is in the same noDoor region as the door entrance node
  28.     for (int i=0; i < door->NumControllers(); i++){
  29.         const CPathNode *sn = g_NodeMap.GetSwitchLocation(door->GetController(i));
  30.         if (sn && sn->NoDoorsBetween(link->Start())){
  31.             bestswitchnode = sn;
  32.             bestswitch = door->GetController(i);
  33.         }
  34.     }
  35.     if (bestswitchnode){ //otherwise, we're going to assume the door is nudgeable
  36.         NewSubgoal(new Goal_HitDoorSwitch(mpAI, bestswitchnode, door)); //nudge the switch
  37.     }
  38.     NewSubgoal(new Goal_GotoNode(mpAI, link->Start()));
  39.     NewSubgoal(new Goal_FollowLink(mpAI, link));
  40.     
  41.     return true;
  42. }
  43.  
  44. // Update the goal
  45. void Goal_GoThroughDoor::Update( float secs_elapsed )
  46. {
  47.     //if we haven't been activated yet, then we need to plan (replan) our subgoals
  48.     if (!active){
  49.         ReplanSubgoals();
  50.         active = true;
  51.         return;
  52.     }
  53.     
  54.    mGoalStatus = UpdateSubgoals( secs_elapsed ); //update subgoals and set our status
  55. }
  56.